package com.yz.jdd.module.main.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.yz.jdd.base.entity.Response_Base;
import com.yz.jdd.base.utils.AppUtils;
import com.yz.jdd.base.utils.DateUtils;
import com.yz.jdd.base.utils.LogUtils;
import com.yz.jdd.base.utils.StringUtil;
import com.yz.jdd.module.main.adapter.CashierOrdersAdapter;
import com.yz.jdd.module.main.adapter.OrdersAdapter;
import com.yz.jdd.network.MySubscriber;
import com.yz.jdd.network.NetWorkRequset;
import com.yz.jdd.network.request.Request_OrderLiuShuiList;
import com.yz.jdd.network.request.Request_OrderTotalList;
import com.yz.jdd.network.response.Response_CashierLiuShuiList;
import com.yz.jdd.network.response.Response_CashierOrderList;
import com.yz.jdd.network.response.Response_FilterCashierTotal;
import com.yz.jdd.network.response.Response_OrderList;
import com.yz.jdd.webview.Router;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class FilterOrderPresenter implements FilterOrderContract.Presenter {


    private Context context;
    private int layoutId;
    private FilterOrderContract.View orderView;
    private OrdersAdapter orderListAdapter;
    private CashierOrdersAdapter cashierOrdersAdapter;


    public String pagesNo = "1";
    public String pageSize = "20";

    public String pagesNoCashierOrder = "1";
    public String pageSizeCashierOrder = "10";

    public String pagesNoLiuShui = "1";
    public String pageSizeLiuShui = "10";


    private List<Response_OrderList.ResultObjectBean.PageDataBean> pageDataBeanList;
    private List<Response_CashierOrderList.PageDataBean> cashPageDataBeanList;


    public FilterOrderPresenter(Context context, int layoutId, FilterOrderContract.View orderView) {
        this.orderView = orderView;
        orderView.setPresenter(this);
        this.context = context;
        this.layoutId = layoutId;

    }

    /**
     * @param groupPosition
     * @param childPosition
     * @param isCash        isCash true 流水 detailType=2 ，false 订单 detailType=1
     */
    @Override
    public void startOrderDetail(int groupPosition, int childPosition, boolean isCash, String orderType) {
        if (isCash) {
            Response_CashierLiuShuiList.PageDataBean orderListBean = cashPageDataBeanList.get(groupPosition).getOrderList().get(childPosition);
            Router.startOrderDetail(context, orderListBean.getOrderId(), orderListBean.getOrderType(),
                    orderListBean.getShopId(), orderListBean.getOrderStatus(), "2", orderType);
        } else {
            Response_OrderList.ResultObjectBean.PageDataBean.OrderListBean orderListBean = pageDataBeanList.get(groupPosition).getOrderList().get(childPosition);
            Router.startOrderDetail(context, orderListBean.getOrderId(), orderType,
                    orderListBean.getShopId(), orderListBean.getOrderStatus() + "", "1", "");
            //  LogUtils.i(orderListBean.getOrderNo() + "/ " + orderListBean.getOrderType() + "/ " + orderListBean.getShopId() + "/ " + orderListBean.getOrderStatus());
        }
    }

    @Override
    public OrdersAdapter getAdapter() {
        if (orderListAdapter == null) {
            synchronized (this) {
                if (pageDataBeanList == null) {
                    pageDataBeanList = new ArrayList<>();
                }
                orderListAdapter = new OrdersAdapter(context, pageDataBeanList);  //orderExtendableListViewAdapter
                orderView.setAdapter();
            }
        }
        return orderListAdapter;
    }

    @Override
    public CashierOrdersAdapter getCashAdapter() {
        if (cashierOrdersAdapter == null) {
            synchronized (this) {
                if (cashPageDataBeanList == null) {
                    cashPageDataBeanList = new ArrayList<>();
                }
                cashierOrdersAdapter = new CashierOrdersAdapter(context, cashPageDataBeanList);  //orderExtendableListViewAdapter
                orderView.setCashAdapter();
            }
        }
        return cashierOrdersAdapter;
    }

    @Override
    public void doGetOrderList(final boolean isRefresh,String bizType, String payType, String orderStatus, String orderSource, String shopId, String startTime, String entTime, String orderType, String deviceId, String operatorId) {

        if (isRefresh) {
            pagesNo = "1";
        }
        Request_OrderTotalList request_orderTotalList = new Request_OrderTotalList();
        request_orderTotalList.setCurrentPage(pagesNo);
        request_orderTotalList.setPageSize(pageSize);
        if (StringUtil.isEmpty(orderType)) {
            request_orderTotalList.setOrderType("1");  //        1.pos 2.外卖 3.异常
            LogUtils.i("111111111" + orderType);
        } else {
            request_orderTotalList.setOrderType(orderType);  //        1.pos 2.外卖 3.异常
            LogUtils.i("222222222222" + orderType);
        }


        // -6 最近前7天的数据
        if (StringUtil.isEmpty(startTime)) {
            startTime = DateUtils.getOldDate(-6);
        }
        if (StringUtil.isEmpty(entTime)) {
            entTime = DateUtils.getOldDate(0);
        }
        request_orderTotalList.setStartTime(AppUtils.getStringToDate(startTime));
        request_orderTotalList.setEndTime(AppUtils.getStringToDate(entTime));
        if (!StringUtil.isEmpty(bizType)) {
            request_orderTotalList.setBizType(bizType);
        }
        if (!StringUtil.isEmpty(payType)) {
            request_orderTotalList.setPayType(payType);
        }
        if (!StringUtil.isEmpty(orderStatus)) {
            request_orderTotalList.setOrderStatus(orderStatus);
        }
        if (!StringUtil.isEmpty(orderSource)) {  // 订单没有 , 流水的订单来源
            request_orderTotalList.setOrderSource(orderSource);
        }
        if (!StringUtil.isEmpty(deviceId)) {
            request_orderTotalList.setDeviceId(deviceId);
        }
        if (!StringUtil.isEmpty(operatorId)) {
            request_orderTotalList.setOperatorId(operatorId);
        }
        if (!StringUtil.isEmpty(shopId)) {
            request_orderTotalList.setShopId(shopId);
        }
        //  orderView.showLoading("加载中");
        try {
            NetWorkRequset.doOrderList(new MySubscriber() {
                @Override
                public void onSuccess(Response_Base response_baseModel) {
                    LogUtils.iLong("返回 doOrderList json ; " + response_baseModel.getResultObject());
                    //返回权限以及user
                    orderView.dismissLoading();
                    try {
                        String json = new Gson().toJson(response_baseModel);
                        Response_OrderList responseOrderLists = new Gson().fromJson(json, Response_OrderList.class);
                        if (isRefresh) {
                            if (pageDataBeanList != null) {
                                pageDataBeanList.clear();
                            } else {
                                pageDataBeanList = new ArrayList<>();
                            }
                            pageDataBeanList.addAll(responseOrderLists.getResultObject().getPageData());
                        } else {
                            if (pageDataBeanList.get(pageDataBeanList.size() - 1).getDate().equals(responseOrderLists.getResultObject().getPageData().get(0).getDate())) {
                                pageDataBeanList.get(pageDataBeanList.size() - 1).getOrderList().addAll(responseOrderLists.getResultObject().getPageData().get(0).getOrderList());
                                responseOrderLists.getResultObject().getPageData().remove(0);
                                pageDataBeanList.addAll(responseOrderLists.getResultObject().getPageData());
                            } else {
                                pageDataBeanList.addAll(responseOrderLists.getResultObject().getPageData());
                            }

                        }
                        getAdapter().notifyDataSetChanged();

                        if ((int) responseOrderLists.getResultObject().getNextPage() > 1) {
                            // 加载更多
                            pagesNo = (int) responseOrderLists.getResultObject().getNextPage() + "";
                            orderView.More();
                        } else {
                            // 没有更多
                            orderView.noMore();
                        }
                        if (pageDataBeanList.size() == 0) {
                            orderView.noData();
                        } else {
                            orderView.Data();
                        }
                        orderView.refreshComplete(isRefresh);
                        orderView.expandGroup(pageDataBeanList.size());
                    } catch (Exception e) {
                        LogUtils.i("jmliu : >>> Exception " + e.toString());
                    }


                }

                @Override
                public void onNeedLogin(String msg) {
                    orderView.dismissLoading();
                    orderView.refreshComplete(isRefresh);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    orderView.dismissLoading();
                    LogUtils.i("onCompleted");
                    orderView.refreshComplete(isRefresh);

                }

                @Override
                public void onFail(Response_Base response_baseModel) {
                    orderView.dismissLoading();
                    orderView.refreshComplete(isRefresh);
                    orderView.showToastDialog(response_baseModel.getMsg());
                    LogUtils.i("onFail" + response_baseModel.getMsg());
                }
            }, request_orderTotalList);
        } catch (Exception e) {
            LogUtils.i("exception" + e.toString());
            orderView.dismissLoading();
            orderView.showToastDialog("系统异常");
        }


    }

    String filterStartTime;
    String filterEndTime;

    //获取收银订单汇总列表
    @Override
    public void doGetFilterCashierOrderList(final boolean isRefresh, String bizType,String payType, String orderStatus,
                                            String orderSource, String shopId, final String startTime,final  String entTime, String deviceId, String operatorId) {

        if (isRefresh) {
            pagesNoCashierOrder = "1";
        }
        Request_OrderLiuShuiList request_orderLiuShuiList = new Request_OrderLiuShuiList();
        request_orderLiuShuiList.setCurrentPage(pagesNoCashierOrder);
        request_orderLiuShuiList.setPageSize(pageSizeCashierOrder);

        // -6 最近前7天的数据
        if (StringUtil.isEmpty(startTime)) {
            filterStartTime = DateUtils.getOldDate(-6);
        } else {
            filterStartTime = startTime;
        }
        filterStartTime = AppUtils.getStringToDate(filterStartTime );
        if (StringUtil.isEmpty(entTime)) {
            filterEndTime = DateUtils.getOldDate(0);
        } else {
            filterEndTime = entTime;
        }
        filterEndTime = AppUtils.getStringToDate(filterEndTime);
        request_orderLiuShuiList.setStartTime(filterStartTime);
        request_orderLiuShuiList.setEndTime(filterEndTime);
        if (!StringUtil.isEmpty(bizType)) {
            request_orderLiuShuiList.setBizType(bizType);
        }
        if (!StringUtil.isEmpty(payType)) {
            request_orderLiuShuiList.setPayType(payType);
        }
        if (!StringUtil.isEmpty(orderStatus)) {
            //流水 - TradeType
            request_orderLiuShuiList.setTradeType(orderStatus);
        }
        if (!StringUtil.isEmpty(orderSource)) {
            request_orderLiuShuiList.setOrderSource(orderSource);
        }
        if (!StringUtil.isEmpty(deviceId)) {
            request_orderLiuShuiList.setDeviceId(deviceId);
        }
        if (!StringUtil.isEmpty(operatorId)) {
            request_orderLiuShuiList.setOperatorId(operatorId);
        }
        if (!StringUtil.isEmpty(shopId)) {
            request_orderLiuShuiList.setShopId(shopId);
        }
        try {
            NetWorkRequset.doOrderLiuShuiList(new MySubscriber() {
                @Override
                public void onSuccess(Response_Base response_baseModel) {
                    LogUtils.iLong("返回json  doChoiceTotal; " + response_baseModel.getResultObject());
                    orderView.dismissLoading();
                    try {
                        Response_FilterCashierTotal filterCashierTotal = (Response_FilterCashierTotal) response_baseModel.getResultObject();

//                        Response_CashierOrderList responseCashierOrderList = new Response_CashierOrderList();
//                        String json = new Gson().toJson(response_baseModel.getResultObject());
//                        LogUtils.iLong("返回json ; " + json);
                        if (isRefresh) {
                            if (cashPageDataBeanList != null) {
                                cashPageDataBeanList.clear();
                            } else {
                                cashPageDataBeanList = new ArrayList<>();
                            }
                            if (StringUtil.isEmpty(entTime)) {
                                cashPageDataBeanList.add(new Response_CashierOrderList.PageDataBean(filterCashierTotal.getTotalGatherModel().getIncomeCnt(), filterCashierTotal.getTotalGatherModel().getIncomeAmount(),
                                        filterCashierTotal.getTotalGatherModel().getRefundCnt(), filterCashierTotal.getTotalGatherModel().getRefundAmount(), startTime + " 至 " +  DateUtils.getOldDate(0)));
                            }else{
                                cashPageDataBeanList.add(new Response_CashierOrderList.PageDataBean(filterCashierTotal.getTotalGatherModel().getIncomeCnt(), filterCashierTotal.getTotalGatherModel().getIncomeAmount(),
                                        filterCashierTotal.getTotalGatherModel().getRefundCnt(), filterCashierTotal.getTotalGatherModel().getRefundAmount(), startTime + " 至 " + entTime));
                            }
                        }
                        if (isRefresh) {
                            //添加到父集合 , 赋值
                            cashPageDataBeanList.get(0).setOrderList(filterCashierTotal.getDataList().getPageData());
                        } else {
                            cashPageDataBeanList.get(0).getOrderList().addAll(filterCashierTotal.getDataList().getPageData());
                        }


                        if ((int) filterCashierTotal.getDataList().getNextPage() > 1) {
                            // 加载更多
                            cashPageDataBeanList.get(0).setMore(true);
                            pagesNoCashierOrder = (int) filterCashierTotal.getDataList().getNextPage() + "";
                        } else {
                            // 没有更多
                            cashPageDataBeanList.get(0).setMore(false);
                        }

                        getCashAdapter().notifyDataSetChanged();

                        if (isRefresh) {
                            orderView.expandGroup();
                        }


                        getCashAdapter().notifyDataSetChanged();
//
                        orderView.noMore();

                        orderView.refreshComplete(isRefresh);
                    } catch (Exception e) {
                        LogUtils.i("jmliu : >>> Exception " + e.toString());
                    }
                }

                @Override
                public void onNeedLogin(String msg) {
                    orderView.dismissLoading();
                    orderView.refreshComplete(isRefresh);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    orderView.dismissLoading();
                    LogUtils.i("onCompleted");
                    orderView.refreshComplete(isRefresh);
                }

                @Override
                public void onFail(Response_Base response_baseModel) {
                    orderView.dismissLoading();
                    orderView.refreshComplete(isRefresh);
                    orderView.showToastDialog(response_baseModel.getMsg());
                    LogUtils.i("onFail" + response_baseModel.getMsg());
                }
            }, request_orderLiuShuiList);
        } catch (Exception e) {
            LogUtils.e("exception" + e.toString());
            orderView.dismissLoading();
            orderView.showToastDialog("系统异常");
        }
    }



}
